﻿using System;
using System.Collections.Generic;
using System.Configuration;
using Citi.PTSConsolidation.Domain;
using Citi.PTSConsolidation.Domain.Utils;
using Citi.RoutineBase;

namespace Citi.PTSConsolidation.SchedulerUI
{
    public class ApplicationDetailExtraction : IExecutableRoutine
    {
        public IEnumerable<RoutineParameter> Parameters { get { return null; } }
        public static string PtsConnectionString { get { return ConfigurationManager.AppSettings["PtsConnectionString"]; } }

        public static void ExtractData(ref ICurrentAction currentAction, IDictionary<string, object> parameters)
        {
            currentAction.Description = "Starting Application Extraction";

            currentAction.Description = "Dropping previous data";
            var queryDeleteDuplicated = Application.QueryDuplicateData;
            currentAction.Description = queryDeleteDuplicated;
            SqlHelper.ExecuteReader(queryDeleteDuplicated, PtsConnectionString);

            currentAction.StartLog("Extracting");
            ExtractDataFromEntrepot(currentAction, parameters);
            currentAction.StopLog();

            currentAction.Description = "Application Extraction Finished";
        }

        private static void ExtractDataFromEntrepot(ICurrentAction currentAction, IDictionary<string, object> parameters)
        {
            currentAction.Description = "Application Extraction: Copy has started";

            var robotUsername = parameters["RobotUsername"].ToString();
            var robotPassword = parameters["RobotPassword"].ToString();

            var dwReportingConnectionString = ConfigurationManager.AppSettings["DWReportingConnectionString"];

            var query = Application.EntrepotQueryString;

            currentAction.Description = query;
            SqlHelper.CopyDataFromAnotherDatabase(dwReportingConnectionString, query, "Application", PtsConnectionString, Application.ColumnMappings);

            currentAction.Description = "Application Extraction: Copy has finished";
        }

        public void Rollback(ref ICurrentAction currentAction, Dictionary<string, object> parameters)
        {
            Stop(ref currentAction, parameters);
        }

        public void Start(DateTime scheduledDate, ref ICurrentAction currentAction, Dictionary<string, object> parameters)
        {
            ExtractData(ref currentAction, parameters);
        }

        public void Stop(ref ICurrentAction currentAction, Dictionary<string, object> parameters)
        {

        }
    }
}
